package com.microblog.microblogsystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.microblog.microblogsystem.domain.dto.UserLoginDTO;
import com.microblog.microblogsystem.domain.dto.UserRegisterDTO;
import com.microblog.microblogsystem.domain.po.User;
import com.microblog.microblogsystem.domain.vo.UserLoginVO;
import com.microblog.microblogsystem.result.Result;
import com.microblog.microblogsystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-06-28
 */
@RestController
@RequestMapping("/user/user")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    private final UserService userService;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);
        return Result.success(userService.login(userLoginDTO));
    }

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册：{}", userRegisterDTO);
        User user = BeanUtil.copyProperties(userRegisterDTO, User.class);
        user.setCreatetime(LocalDate.now());
        userService.save(user);
        return Result.success();
    }
}
